<?php
/**
 * Created by PhpStorm.
 * User: JSQ-SL
 * Date: 2018/11/19
 * Time: 15:37
 */

namespace app\index\table;

use app\index\controller\Login;
use think\Db;
use app\index\service\ExcelService;

class FacilityTable extends BaseTable
{
    protected static $view = 'V_ASSET_EQUIP_FACILITY';
    protected static $table = 'ASSET_EQUIP_FACILITY';
    protected static $pk = 'FACILITY_NO';
    protected static $configTable = 'ASSET_EQUIP_FACILITY_CONFIG';

    static public function table(){
        return Db::table(static::$view);
    }

    static public function add($data){
        $data[self::$pk] = static::getNextId();
        $data['OPERATOR'] = Login::getUserId();
        $data['INPUTDATE'] = date('Y-m-d');

        self::toDate($data,'ENTERTIME');
        self::toDate($data,'OUTFACTORY_DATE');
        self::toDate($data,'INPUTDATE');
        Db::table(static::$table)->insert($data);
        return $data[self::$pk];
    }

    static public function addImport($data){
        $data['OPERATOR'] = Login::getUserId();
        $data['INPUTDATE'] = date('Y-m-d');

        self::toDate($data,'ENTERTIME');
        self::toDate($data,'OUTFACTORY_DATE');
        self::toDate($data,'INPUTDATE');
        Db::table(static::$table)->insert($data);
    }

    static public function update($where,$data){
        self::toDate($data,'ENTERTIME');
        self::toDate($data,'OUTFACTORY_DATE');
        Db::table(static::$table)->where($where)->update($data);
    }

    static public function delete($where){
        return Db::table(static::$table)->where($where)->delete();
    }

    public static function getConfigAdd(){
        return Db::table(static::$configTable)->where(['ADD_FLAG'=>'1'])->order('ID ASC')->select();
    }

    public static function getConfigEdit(){
        return Db::table(static::$configTable)->where(['EDIT_FLAG'=>'1'])->order('ID ASC')->select();
    }

    public static function getNextId(){
        $year = date('Y');
        $numMaxRecord = Db::table(static::$table)
            ->field('max(SUBSTR('.static::$pk.',5,6)) as NUM')
            ->where([['SUBSTR('.static::$pk.',1,4)','=',$year]])
            ->fetchSql(true)
            ->find();
        $numMaxRecord = Db::query($numMaxRecord);
        $num = $numMaxRecord[0]['NUM'];
        if ($num == NULL) {
            $num = 0;
        }
        $num++;
        return $year.sprintf("%06d", $num);
    }

    public static function exportExcel($data,$fileName){

        $fieldList = [
            ['title'=>'设备号','field'=>'FACILITY_NO'],
            ['title'=>'入账时间','field'=>'ENTERTIME'],
            ['title'=>'设备名称','field'=>'FACILITY'],
            ['title'=>'设备分类','field'=>'FACITYPE'],
            ['title'=>'所属单位','field'=>'DEPART_BELONG'],
            ['title'=>'使用单位','field'=>'DEPART_USE'],
            ['title'=>'国别','field'=>'COUNTRY_CNNAME'],
            ['title'=>'型号','field'=>'MODEL'],
            ['title'=>'技术指标','field'=>'TECH_SPEC'],
            ['title'=>'单价','field'=>'PRICE'],
            ['title'=>'厂家','field'=>'FACTORY'],
            ['title'=>'出厂日期','field'=>'OUTFACTORY_DATE'],
            ['title'=>'出厂号','field'=>'OUTFACTORY_NO'],
            ['title'=>'使用方向','field'=>'USEDIRECTION'],
            ['title'=>'经费科目','field'=>'FININ'],
            ['title'=>'单据号','field'=>'RECEIPT_NO'],
            ['title'=>'保管人','field'=>'PERSON_GET'],
            ['title'=>'经办人','field'=>'PERSON_THROUGH'],
            ['title'=>'室设备管理员','field'=>'PERSON_CHECK'],
            ['title'=>'是否开放(0,不开放;1,开放)','field'=>'IS_OPEN'],
            ['title'=>'详细技术指标','field'=>'TECH_SPEC_DETAIL'],
            ['title'=>'功能及应用范围','field'=>'FUNCTION_USE'],
            ['title'=>'可供开放时间','field'=>'OPEN_TIME'],
            ['title'=>'收费标准','field'=>'FEE_STANDARD'],
            ['title'=>'设备存放地点','field'=>'PLACE_STORE'],
            ['title'=>'设备负责人','field'=>'PERSON_RESPONSE'],
            ['title'=>'联系电话','field'=>'PHONE'],
            ['title'=>'电子邮件','field'=>'EMAIL'],
            ['title'=>'图片','field'=>'PHOTO'],
            ['title'=>'操作员','field'=>'OPERATOR'],
            ['title'=>'录入日期','field'=>'INPUTDATE'],
            ['title'=>'是否审核(0,未审核;1,已审核)','field'=>'IS_AUDIT'],
            ['title'=>'设备状态','field'=>'STATE'],
            ['title'=>'备注','field'=>'COMMENTS'],
            ['title'=>'设备所属详细科室','field'=>'DEPA_PARTICULAR'],
        ];

        $excel = new ExcelService();
        $excel->exportExcel($data,$fileName,$fieldList);
    }

    const FACILITY_FIELD_LIST = [
        "FACILITY_NO","ENTERTIME","FACILITY","FACITYPE_ID","DEPART_BELONG","DEPART_USE","COUNTRY_ID","MODEL","TECH_SPEC","PRICE","FACTORY","OUTFACTORY_DATE","OUTFACTORY_NO","USEDIRECTION_ID","FININ_ID","RECEIPT_NO","PERSON_GET","PERSON_THROUGH","PERSON_CHECK","IS_OPEN","TECH_SPEC_DETAIL","FUNCTION_USE","OPEN_TIME","FEE_STANDARD","PLACE_STORE","PERSON_RESPONSE","PHONE","EMAIL","PHOTO","OPERATOR","INPUTDATE","IS_AUDIT","STATE_ID","COMMENTS","DEPA_PARTICULAR",
    ];
}